#! /usr/bin/env bash
set -e
_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
cd $_dir

n_threads=$(nproc --all)

# Assume 64-bit (only 64-bit is supported)
os=
arch=

case $(uname -m) in
    x86_64) arch=x86 ;;
    arm64) arch=arm ;;
    aarch64) arch=arm ;;
    
    *)
        echo "Unsupported architecture '$(uname -m)'"
        exit 1 
    ;;
esac

case "$OSTYPE" in
    "linux-gnu"*) os=linux ;;
    "darwin"*) os=macos ;;

    *)
        echo "Unsupported OS '$OSTYPE'"
        exit 1 
    ;;

esac


# Build
pushd modules/bls-signatures

mkdir -p build
cd build
cmake ../
cmake --build . -- -j$n_threads

popd



# Copy headers & library
rm -rf lib/include/bls
rm -rf lib/include/relic
mkdir -p lib/include/bls
mkdir -p lib/include/relic

cp -r modules/bls-signatures/build/_deps/relic-src/include/* lib/include/relic
cp -r modules/bls-signatures/build/_deps/relic-build/include/* lib/include/relic
cp -r modules/bls-signatures/src/*.hpp lib/include/bls/

tgt_dir=lib/$os/$arch
mkdir -p $tgt_dir

cp modules/bls-signatures/build/libbls.a $tgt_dir/



